﻿package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.badibadi.abcpinying.SortModel;
import com.badibadi.infos.CreateNewClubModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.serializable.CreationNewClubData;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 张贴链接 create_club_number1
 * 
 * @author Administrator
 * 
 */
public class CreationNewClub3Activity extends BaseActivity implements
		OnClickListener {

	private String KEY = "CreationNewClub3Activity";
	private String TAG = "CreationNewClub3Activity";
	private EditText enterLianjie;
	private TextView lianjie;
	private CreateNewClubModel cNew;
	private int What = -1;
	EditText price, limitnum;// 所需金币，人数上限
	CheckBox show_me_the_dongtai;
	private Results results;

	private final String KEY_clubid = "clubid";
	private final String KEY_ret = "ret";

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(CreationNewClub3Activity.this);
				try {
					Utils.showMessage(CreationNewClub3Activity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 2:

				try {
					Utils.ExitPrgress(CreationNewClub3Activity.this);
					Utils.showMessage(CreationNewClub3Activity.this,
							getResources().getString(R.string.l_xb55));
					Intent intent = new Intent(CreationNewClub3Activity.this,
							CreationNewClub4Activity.class);
					intent.putExtra("clubname", cNew.getName());
					intent.putExtra("clubid", results.getRetmsg());
					intent.putExtra("uid", cNew.getUid());
//					   
					startActivity(intent);
					overridePendingTransition(R.anim.slide_left,
							R.anim.slide_right);
					CreationNewClub3Activity.this.finish();
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				Utils.ExitPrgress(CreationNewClub3Activity.this);

				// onLoad();
				try {
					Utils.showMessage(CreationNewClub3Activity.this,
							getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 4:
				Utils.ExitPrgress(CreationNewClub3Activity.this);
				try {
					// Utils.showMessage(PinyinSeeOtherPeopleFriendActivity.this,
					// results2.getRetmsg());
				} catch (Exception e) {
					// TODO: handle exception
				}
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.creation_new_club4);

		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});

		CreateNewClubModel cc = (CreateNewClubModel) getIntent()
				.getSerializableExtra("createNewClubModel");
		show_me_the_dongtai = (CheckBox) this
				.findViewById(R.id.show_me_the_dongtai);
		if (cc != null)
			cNew = cc;
		//    
		price = (EditText) this.findViewById(R.id.price);// 所需金币
		limitnum = (EditText) this.findViewById(R.id.limitnum);// 人数上限
		this.findViewById(R.id.create_club_number1).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// 这里提交数据
						Intent intent = new Intent(
								CreationNewClub3Activity.this,
								CreationNewClub4Activity.class);
						startActivity(intent);
						overridePendingTransition(R.anim.slide_left,
								R.anim.slide_right);
					}
				});
		this.findViewById(R.id.ztlj_sina).setOnClickListener(this);
		this.findViewById(R.id.ztlj_facebook).setOnClickListener(this);
		this.findViewById(R.id.ztlj_weixin).setOnClickListener(this);
		this.findViewById(R.id.ztlj_linkedin).setOnClickListener(this);
		this.findViewById(R.id.ztlj_qq).setOnClickListener(this);
		this.findViewById(R.id.ztlj_twitter).setOnClickListener(this);
		this.findViewById(R.id.ztlj_AddNew).setOnClickListener(this);
		this.findViewById(R.id.create_club_number1).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ztlj_sina:
			ztlj(0);
			break;
		case R.id.ztlj_facebook:
			ztlj(1);
			break;
		case R.id.ztlj_weixin:
			ztlj(2);
			break;
		case R.id.ztlj_linkedin:
			ztlj(3);
			break;
		case R.id.ztlj_qq:
			ztlj(4);
			break;
		case R.id.ztlj_twitter:
			ztlj(5);
			break;
		case R.id.ztlj_AddNew:
			ztlj(6);
			break;
		case R.id.create_club_number1:
			cNew.setPrice("" + price.getText().toString());
			if (!StringUtil.isNullOrEmpty(price.getText().toString())) {
				cNew.setIs_gold("1");
			} else {
				cNew.setIs_gold("0");
			}
			cNew.setLimitnum("" + limitnum.getText().toString());
			if (!StringUtil.isNullOrEmpty(limitnum.getText().toString())) {
				cNew.setIs_person("1");
			} else {
				cNew.setIs_person("0");
			}

			if (show_me_the_dongtai.isChecked() == true)
				cNew.setIs_show("1");
			else
				cNew.setIs_show("0");

			CreateMyClub();

			break;
		}
	}

	/**
	 * 在这里提交数据
	 */
	private void CreateMyClub() {
//		   
		// TODO Auto-generated method stub
		Utils.showPrgress(CreationNewClub3Activity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {

				Map<String, Object> map = new HashMap<String, Object>();
				try {
					map.put("arr_name", fengzhuang1());
					map.put("lang",
							com.badibadi.mytools.Dialog
									.getSystemLanguageTypegy(CreationNewClub3Activity.this));
//					   
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					   
				}
				
//				   ("创建俱乐部的参数是:" + map.toString());
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl_1 + "club/addClub");
				if (invite_content != null) {
					results = Utils.checkResult_NNN(
							CreationNewClub3Activity.this, invite_content);
//					   
//					   ("创建俱乐部返回的数据是:" + invite_content + " "
//							+ results.getRetmsg());
					if (results != null && !results.getRetmsg().equals("null")
							&& results.isRet()) {
//						   
						// Message msg = new Message();
						// msg.what = 2;
						// Bundle bun = new Bundle();
						// bun.putString(KEY_clubid, results.getRetmsg());
						// bun.putBoolean(KEY_ret, results.isRet());
						// msg.setData(bun);
						mHandler.sendEmptyMessage(2);
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {

					mHandler.sendEmptyMessage(1);
				}

			}
		});

	}

	private JSONObject fengzhuang() throws JSONException {
		JSONObject js = new JSONObject();
		js.put("arr_name", fengzhuang1());
		// js.put("link",fengzhuang2());
		return js;

	}

	private JSONObject fengzhuang1() throws JSONException {

		JSONObject object1 = new JSONObject();
		object1.put("tid", cNew.getTid());
		object1.put("uid", cNew.getUid());// cNew.getUid()
		object1.put("name", cNew.getName());
		//    
		object1.put("info", cNew.getInfo());
		object1.put("room_head", cNew.getRoom_head());
		object1.put("price", cNew.getPrice());
		object1.put("is_gold", cNew.getIs_gold());
		object1.put("room_limit", cNew.getRoom_limit());
		object1.put("is_person", cNew.getIs_person());
		object1.put("limitnum", cNew.getLimitnum());
		object1.put("originate_address", cNew.getOriginate_address());
		object1.put("suit_persons", cNew.getSuit_persons());
		object1.put("logo", cNew.getLogo());
		object1.put("map_x", cNew.getMap_x());
		object1.put("map_y", cNew.getMap_y());
		object1.put("is_apply", cNew.getIs_apply());
		if (StringUtil.isNullOrEmpty(cNew.getHttp_weibo())) {
			object1.put("sina_url", "");
		} else {
			object1.put("sina_url", "" + cNew.getHttp_weibo());
		}
		if (StringUtil.isNullOrEmpty(cNew.getHttp_facebook())) {
			object1.put("facebook_url", "");
		} else {
			object1.put("facebook_url", "" + cNew.getHttp_facebook());
		}
		if (StringUtil.isNullOrEmpty(cNew.getHttp_twitter())) {
			object1.put("twitter_url", "");
		} else {
			object1.put("twitter_url", "" + cNew.getHttp_twitter());
		}
		if (StringUtil.isNullOrEmpty(cNew.getHttp_weixin())) {
			object1.put("WeChat_url", "");
		} else {
			object1.put("WeChat_url", "" + cNew.getHttp_weixin());
		}
		if (StringUtil.isNullOrEmpty(cNew.getHttp_qq())) {
			object1.put("qq_url", "");
		} else {
			object1.put("qq_url", "" + cNew.getHttp_qq());
		}
		if (StringUtil.isNullOrEmpty(cNew.getHttp_linkedin())) {
			object1.put("google_url", "");
		} else {
			object1.put("google_url", "" + cNew.getHttp_linkedin());
		}
		if (StringUtil.isNullOrEmpty(cNew.getHttp_AddNewLink())) {
			object1.put("other_url", "");
		} else {
			object1.put("other_url", "" + cNew.getHttp_AddNewLink());
		}

		object1.put("is_show", cNew.getIs_show());

		// object1.put("link", fengzhuang2());
		// array_name.put("object",object);
//		   
		return object1;
	}

	private JSONObject fengzhuang2() throws JSONException {

		JSONObject object = new JSONObject();

		object.put("sina", cNew.getHttp_weibo());
		object.put("facebook", cNew.getHttp_facebook());
		object.put("twitter", cNew.getHttp_twitter());
		object.put("weixin", cNew.getHttp_weixin());
		object.put("qq", cNew.getHttp_qq());
		object.put("google", cNew.getHttp_linkedin());
		object.put("diy", cNew.getHttp_AddNewLink());

		return object;
	}

	/**
	 * 字体链接 0 新浪 1 Facebook 2 微信 3，linkedin 4.qq 。5.twitter 6额外新加的
	 */
	public void ztlj(final int num) {
		new AlertDialog.Builder(CreationNewClub3Activity.this)
				.setTitle(getResources().getString(R.string.PleaseDrawLianjie_))
				.setView(getView(num))
				.setNegativeButton(getResources().getString(R.string.ok),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								String uri = lianjie.getText().toString()
										.trim()
										+ enterLianjie.getText().toString()
												.trim();
								if (cNew != null) {
									switch (num) {
									case 0:
										cNew.setHttp_weibo(uri);
//										   
//												"getHttp_weibo===>"
//														+ cNew.getHttp_weibo());

										break;
									case 1:
										cNew.setHttp_facebook(uri);
//										   
//												+ cNew.getHttp_facebook());
										break;
									case 2:
										cNew.setHttp_weixin(uri);
//										   
//												"getHttp_weixin===>"
//														+ cNew.getHttp_weixin());
										break;
									case 3:
										cNew.setHttp_linkedin(uri);
//										   
//												+ cNew.getHttp_linkedin());
										break;
									case 4:
										cNew.setHttp_qq(uri);
//										   
//												"getHttp_qq===>"
//														+ cNew.getHttp_qq());
										break;
									case 5:
										cNew.setHttp_twitter(uri);
//										   
//												"getHttp_twitter===>"
//														+ cNew.getHttp_twitter());
										break;
									case 6:
										cNew.setHttp_AddNewLink(uri);
//										   
//												+ cNew.getHttp_AddNewLink());
										// 已经新增加
										break;

									}
									ShowToast(
											CreationNewClub3Activity.this,
											getResources().getString(
													R.string.l_xb66)
													+ ":" + uri);
								}

							}
						})
				.setPositiveButton(getResources().getString(R.string.Return),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						}).show();
	}

	/**
	 * http://www.weibo.com/=>微薄 http://www.twitter.com/=>twitter
	 * https://www.facebook.com/=>facebook QQ=>就QQ號 微信=>就微信號
	 * http://www.linkedin.com/=>linkedin 然後 新增連結 就全空白
	 * 
	 * @param num
	 *            0 新浪 1 Facebook 2 微信 3，linkedin 4.qq 。5.twitter
	 * @return
	 */
	private LinearLayout getView(int num) {
		// TODO Auto-generated method stub
		LinearLayout ll = new LinearLayout(CreationNewClub3Activity.this);
		ll.setOrientation(LinearLayout.HORIZONTAL);

		lianjie = new TextView(CreationNewClub3Activity.this);
		switch (num) {
		case 0:
			lianjie.setText("" + HTTP_WEIBO);
			break;
		case 1:
			lianjie.setText("" + HTTP_FACEBOOK);
			break;
		case 2:
			lianjie.setText("" + HTTP_WEIXIN);
			break;
		case 3:
			lianjie.setText("" + HTTP_LINKEDIN);
			break;
		case 4:
			lianjie.setText("" + HTTP_QQ);
			break;
		case 5:
			lianjie.setText("" + HTTP_TWITTER);
			break;
		case 6:
			lianjie.setText("");
			break;
		}
		What = num;
		enterLianjie = new EditText(CreationNewClub3Activity.this);
		enterLianjie.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(
						CreationNewClub3Activity.this, 50)));
		enterLianjie.setGravity(Gravity.CENTER_VERTICAL);
		ll.addView(lianjie);
		ll.addView(enterLianjie);
		return ll;
	}

}
